--
-- Author: shunguo.chen
-- Date: 2016-10-19 17:42:49
--
-- RewardSignPanel
local ClassRef = app.mvc.createPanelClass(...)
local AvatarFactory = app:getClass("AvatarFactory")

--data =
function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardSignPanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end
function ClassRef.create(node)
	return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
	self.super.init(self, data)
	self:setContentSize( self._ui.Panel:getContentSize() )

	self._pageView = self._ui.PageView

	self._ui.btn_left:addClickEventListener(function()
		self:_goPrePage()
	end)

	self._ui.btn_right:addClickEventListener(function()
		self:_goNextPage()
	end)

    --默认第一页
    self._ui.btn_left:setVisible(false)

	self._signRenderNode = self._ui["Panel/page"]
	self._signRenderNode:setVisible(false)
	self._haveInitPageView = false
	--显示坐骑的动画节点
	self._pageDatas = {}
	self._playIndex = {}


	self:refresh()
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:refresh()
	if self._data then
		local dayAwards = self._data.dayAwards

		if not self._haveInitPageView then
			self._pageView:removeAllPages()
			for k,v in ipairs(dayAwards) do
				local render = self._signRenderNode:clone()
				self:setPageData(render,v)
				self._pageView:addPage(render)
			end

			if #dayAwards == 1 then
				self._ui.btn_right:setVisible(false)
			end

			self._haveInitPageView = true
		else
			local allPages = self._pageView:getPages()
			for k,v in ipairs(allPages) do
				self:setPageData(v,dayAwards[k])
			end
        end

		self:setVisible( true )
	else
		self:setVisible( false )
	end
end

function ClassRef:setPageData(pageNode,data)

	pageNode:findChild("Image"):loadTexture(Res.activityIconPath..data.iconName..".png")

	pageNode:findChild("tlt"):setString(L(string.format("sign7reward%d",data.idx)))
	pageNode:findChild("text"):setString(data.des)
	local receiveBtn = pageNode:findChild("btn_receive")
    local mountNode = pageNode:getChildByName("Node_mount")

    --初始化的时候做
	if not self._haveInitPageView then
		receiveBtn:addClickEventListener(function()
			--准备领取奖励
			executeFunc(self._onOperate, "getSignAward", {idx = data.idx,uuid = self._data.uuid})
		end)

        display.nodeTouchOneByOne(mountNode, function(posX,posY)
            if self:isVisible() then
                local armature = mountNode:getChildByTag(0)
                if armature and armature:containWorldPoint(posX, posY) then
                    self:_playAnimation(armature, true, data.idx)
                end
            end
            return false
        end)

	end
    --刷新的时候做
	if data.haveGet then
		receiveBtn:setEnabled(false)
		pageNode:findChild("btn_receive/name"):setString(L("tips_2000236"))
		receiveBtn:setBright(false)
	else
		receiveBtn:setEnabled(data.canGet)
		receiveBtn:setBright(data.canGet)
		pageNode:findChild("btn_receive/name"):setString(L("tips_2000235"))
    end


    local oldData = self._pageDatas[data.idx]
    local newData = data.award
    if newData then
		self._pageDatas[data.idx] = newData
        if not oldData or oldData.type ~= newData.type or oldData.id ~= newData.id then
            mountNode:removeAllChildren()
            local armature = nil
            if newData.type == Enums.ItemBagType.mount then
                armature = AvatarFactory.createRideById(newData.id, false)
            elseif newData.type == Enums.ItemBagType.mercenary then
                local npcId = GD:getPartnerNpcIdByPartnerId(newData.id,1)
                if npcId then
                    armature = AvatarFactory.createNpcById(npcId, false)
                end
            end

            if armature then
                mountNode:addChild(armature)
                armature:setTag(0)
                self:_playAnimation(armature,false, data.idx)

                local box = armature:getBoundingBox()
                local pos = mountNode:convertToNodeSpace(cc.p(box.x + box.width/2, box.y))
                armature:setPositionX(-pos.x)
            end
        end
    end

	pageNode:setVisible(true)
end

function ClassRef:_goPrePage()
	if not self._data then return end
	local dayAwards = self._data.dayAwards
	if dayAwards then
		local curIdx = self._pageView:getCurPageIndex()
		if curIdx > 0 then
			local targetIdx = curIdx - 1
			self._pageView:scrollToPage(targetIdx)
            if targetIdx == 0 then
                self._ui.btn_left:setVisible(false)
				self._ui.btn_right:setVisible(true)
            else
                self._ui.btn_right:setVisible(true)
            end
		end
	end
end

function ClassRef:_goNextPage()
	if not self._data then return end
	local dayAwards = self._data.dayAwards
	if dayAwards then
		local curIdx = self._pageView:getCurPageIndex()
		if curIdx < #dayAwards-1 then
			local targetIdx = curIdx + 1
			self._pageView:scrollToPage(targetIdx)
            if targetIdx == #dayAwards-1 then
                self._ui.btn_right:setVisible(false)
				self._ui.btn_left:setVisible(true)
            else
                self._ui.btn_left:setVisible(true)
            end
		end
	end
end

function ClassRef:setData(data)
	self._data = data
	self:refresh()
end

--随从的动画 和 坐骑的动画不一样
local MountAnimations = {"ride_idle","ride_move" }
local MercenaryAnimations = {"talk","mapidle"}

function ClassRef:_playAnimation(armature, isNext, idx)
    local data = self._pageDatas[idx]
    if not data then return end
    local animations = nil
    if data.type == Enums.ItemBagType.mount then
        animations = MountAnimations
    elseif data.type == Enums.ItemBagType.mercenary then
        animations = MercenaryAnimations
    end

    if animations then
        if not self._playIndex[idx] then self._playIndex[idx] = 1 end

        isNext = isNext or false
        if isNext then
            self._playIndex[idx] = self._playIndex[idx] + 1
            if self._playIndex[idx] > #animations then
                self._playIndex[idx] = 1
            end
        end
        armature:play(animations[self._playIndex[idx]])
    end

end

return ClassRef